import LGXCOM from '../common/common'
import {CoordTRFUtil} from '../common/tools/coordtool';

import utilfuncs from '../common/tools/utilfuncs';
var funcs = utilfuncs.UtilFuncs;

import { GlobalVar } from '../common/globalvar';

var DisplayGrid = {}

//1,2,4,8,10
DisplayGrid.disPlayGrid = function(map,mapinfo)
{
    if (!mapinfo.getDisGrid() || !mapinfo.getClearViewPortFlag()){
        return ;
    }
    map.save();
    let screenFreshView = mapinfo.getMapWindowRect();

    let sysLeftTopPtPt = CoordTRFUtil.screen2world({x:0,y:0}, mapinfo);

    let x = screenFreshView.width();
    let y = screenFreshView.height();
    let sysRightBotPtPt = CoordTRFUtil.screen2world({x,y}, mapinfo);

    let color = mapinfo.getGridColor();
    map.beginPath();
    let scaling = mapinfo.getScaling();
    let gridGap = mapinfo.getGridSpan();
    let outGridScale = this.getScalingZoone(scaling);
    let actGridGap = gridGap / outGridScale;

    let xstIndex = (sysLeftTopPtPt.x / actGridGap);
    let xEndIndex = (sysRightBotPtPt.x / actGridGap);
    let offsetIndex = 5;
    let gridLinePosX = 0;
    let p1 = {x:0,y:0};
    let p2 = {x:0,y:0};
    for (let i = xstIndex - offsetIndex; i < xEndIndex + offsetIndex; i++)
    {
        gridLinePosX = actGridGap * i;

        p1.x = gridLinePosX;
        p1.y = sysRightBotPtPt.y;
        let sp1 = CoordTRFUtil.world2Screen(p1, mapinfo);

        p2.x = gridLinePosX;
        p2.y = sysLeftTopPtPt.y;
        let sp2 = CoordTRFUtil.world2Screen(p2, mapinfo);
        let rst = this.chkLineInterRect(screenFreshView,sp1,sp2); 
        if (rst && rst.flag && rst.line){
            let dislineSec = rst.line;
            map.moveTo(dislineSec.stPt.x, dislineSec.stPt.y);
            map.lineTo(dislineSec.endPt.x, dislineSec.endPt.y);
        }
    }

    let yEndIndex = (sysLeftTopPtPt.y / actGridGap);
    let yStIndex = (sysRightBotPtPt.y / actGridGap);
    let gridLinePosY = 0;

    for (let i = yStIndex - offsetIndex; i < yEndIndex + offsetIndex; i++)
    {
        gridLinePosY = actGridGap * i;
        p1.x = sysLeftTopPtPt.x;
        p1.y = gridLinePosY;
        let sp1 = CoordTRFUtil.world2Screen(p1, mapinfo);

        p2.x = sysRightBotPtPt.x;
        p2.y = gridLinePosY;
        let sp2 = CoordTRFUtil.world2Screen(p2, mapinfo);
        let rst = this.chkLineInterRect(screenFreshView,sp1,sp2); 
        if (rst && rst.flag && rst.line){
            let dislineSec = rst.line;
            map.moveTo(dislineSec.stPt.x, dislineSec.stPt.y);
            map.lineTo(dislineSec.endPt.x, dislineSec.endPt.y);
        }
    }
    map.strokeStyle = mapinfo.getGridColor();
    map.lineWidth = 1;
    map.stroke();

    map.restore();
}

//1,2,4,8,10
DisplayGrid.getScalingZoone = function(scaling)
{
    let outGridScale = 1;
    let zoneArr = [1, 2, 4, 8, 10];
    let nearScaling = Math.ceil(scaling);
    let sScale = nearScaling.toString();
    let lenth = sScale.length;
    let zoomScale = 1;

    if (lenth == 2)
    {
        zoomScale = 10;
    }
    else if (lenth == 3)
    {
        zoomScale = 100;
    }
    else if (lenth == 4)
    {
        zoomScale = 1000;
    }

    let tempOutZone =  zoneArr[0];
    let temp1 = nearScaling / zoomScale;
    let mindis = Math.abs(temp1 - zoneArr[0]);

    for (let i = 1; i < 5; i++)
    {
        let dis = Math.abs(temp1 - zoneArr[i]);
        if (mindis > dis)
        {
            mindis = dis;
            tempOutZone =  zoneArr[i];
        }
    }

    outGridScale = tempOutZone * zoomScale;
    return outGridScale;
}

DisplayGrid.chkLineInterRect = function(rect,lnPT1,lnPT2)
{
    let outline = new LGXCOM.LGXLineSection();
    let interFlag = false;
    let kind = funcs.chkHornVertKind(lnPT1,lnPT2);
    if (kind == 1){
        if ((lnPT1.y < rect.bottom && lnPT1.y > rect.top)
                ||(lnPT1.y < rect.top && lnPT1.y > rect.bottom)){
            outline.stPt.x = rect.left;
            outline.endPt.x = rect.right;

            outline.stPt.y = lnPT1.y;
            outline.endPt.y = lnPT1.y;
            interFlag = true;
        }
    }
    else if (kind == 2){
        if (lnPT1.x < rect.right && lnPT1.x > rect.left){
            outline.stPt.y = rect.top;
            outline.endPt.y = rect.bottom;

            outline.stPt.x = lnPT1.x;
            outline.endPt.x = lnPT1.x;
            interFlag = true;
        }
    }
    return {flag:interFlag,line:outline};
}

export{DisplayGrid}
